package base.thread_JUC.demo07;

/**
 * 写个死锁程序，并分析排查原因分析.
 *
 * terminal
 * jps -l  查看java进程
 * jstack 进程号
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";

        new Thread(new MyDeadLock(lockA, lockB)).start();
        new Thread(new MyDeadLock(lockB, lockA)).start();


    }
}

class MyDeadLock implements Runnable{
    private String lockA;
    private String lockB;

    public MyDeadLock (String lockA, String lockB){
        this.lockA = lockA;
        this.lockB = lockB;
    }

    public void run(){
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName()+"\t持有 "+lockA+" 锁，尝试获取 "+lockB+" 锁");
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName()+"\t获的 "+lockB+" 锁");
            }
        }
    }
}
